/**
 * @param {number[]} gas
 * @param {number[]} cost
 * @return {number}
 */
var canCompleteCircuit = function (gas, cost) {
    let len = gas.length,
        curSum = 0,
        totalSum = 0,
        curIndex = 0;
    for (let i = 0; i < gas.length; i++) {
        totalSum += gas[i] - cost[i]
        curSum += gas[i] - cost[i]
        if (curSum < 0) {
            curIndex = i + 1
            curSum = 0
        }
    }
    if (totalSum < 0) return -1
    return curIndex
};